package com.ihrm.company.service;

import com.ihrm.common.utils.IdWorker;
import com.ihrm.company.dao.CompanyDao;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 13932105637
 * @version l.0
 * @date 2021/7/6 21:01
 **/
@Service
public class CompanyService {
    private final CompanyDao companyDao;

    private final IdWorker idWorker;

    @Autowired
    public CompanyService(CompanyDao companyDao, IdWorker idWorker) {
        this.companyDao = companyDao;
        this.idWorker = idWorker;
    }

    /**
     * 保存企业
     *  1.配置idwork到工程
     *  2.在service中注入idwork
     *  3.通过idwork生成id
     *  4.保存企业
     */
    public void add(Company company) {
        //基本属性的设置
        String id = idWorker.nextId()+"";
        company.setId(id);
        //默认的状态 0：未审核，1：已审核
        company.setAuditState("0");
        //0.未激活，1：已激活
        company.setState(1);
        companyDao.save(company);
    }

    /**
     * 更新企业
     *  1.参数：Company
     *  2.根据id查询企业对象
     *  3.设置修改的属性
     *  4.调用dao完成更新
     */
    public void update(Company company) {

        if (companyDao.findById(company.getId()).isPresent()) {
            Company temp = companyDao.findById(company.getId()).get();
            temp.setName(company.getName());
            temp.setCompanyPhone(company.getCompanyPhone());
            companyDao.save(temp);
        }

    }


    /**
     * 删除企业
     */
    public void deleteById(String id) {
        companyDao.deleteById(id);
    }

    /**
     * 根据id查询企业
     */
    public Company findById(String id) {
        if (companyDao.findById(id).isPresent()) {
            return companyDao.findById(id).get();
        }
        return null;
    }

    /**
     * 查询企业列表
     */
    public List<Company> findAll() {
        return companyDao.findAll();
    }
}
